package org.yiming.tools.mqtt.client.cli;

import lombok.extern.slf4j.Slf4j;
import org.yiming.tools.mqtt.client.ApplicationContext;
import org.yiming.tools.mqtt.client.client.MqttClient;
import org.yiming.tools.mqtt.client.client.MqttConfig;
import org.yiming.tools.mqtt.client.client.MqttConfigFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * @author renwenpeng
 * date  2022/3/26 1:03 下午
 */
@Slf4j
public class MyMain {
    public static void main(String[] args) {
//        args = new String[]{"dev"};
        List<String> devStrings = Arrays.asList("local", "dev", "demo", "test");
        if (args.length > 0 && devStrings.contains(args[0].toLowerCase())) {
            String envName = args[0].toLowerCase();

            ApplicationContext applicationContext = new ApplicationContext();
            MqttConfigFactory mqttConfigFactory = applicationContext.getMqttConfigFactory();

            Map<String, MqttConfig> mqttConfigMap = new HashMap<>(4);

            mqttConfigMap.put("local"
                    , mqttConfigFactory.newInstance("tcp://127.0.0.1:9530"
                            , "testuser", "123456"));

            ExecutorService threadPoolTaskExecutor = applicationContext.getThreadPoolTaskExecutor();

            MqttClient mqttClientDev1 = new MqttClient(threadPoolTaskExecutor
                    , mqttConfigMap.get(envName)
                    , "alaeat_device:0_riverRoad");
            applicationContext.putMqttClient(mqttClientDev1);

            Application.run(applicationContext, () -> {
                // 群发
                mqttClientDev1.subscribe("alaeat-shop_0/#", 1);
                // 单发
                mqttClientDev1.subscribe("alaeat-device_riverRoad/#", 1);
                return null;
            });
        } else {
            log.warn("args incorrect, Qualifier: {}", String.join(",", devStrings));
        }
    }
}
